package com.dxf.database.dbInfo;

import com.dxf.database.dbDao.DBMetaData;
import com.dxf.database.dbDao.impl.DBMetaDataImpl;
import com.dxf.entity.DBType;
import com.dxf.entity.Table;
import com.dxf.utils.PropertiesUtils;
import com.dxf.utils.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 创建dataBase对象工厂类,
 * 初始化会读取配置文件中的数据源配置
 * 如果数据不再配置源中,也可以单独创建
 */
public class DataBaseFactory {

    protected static String pgsqlUrl = "jdbc:postgresql://[ip]:[port]/[db]?useUnicode=true&amp;characterEncoding=UTF8";

    protected static String sqlServerUrl = "jdbc:sqlserver://[ip]:[port];DatabaseName=[db]";
    protected static String mysqlUrl = "jdbc:mysql://[ip]:[port]/[db]?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true";
    protected static String oracleUrl = "jdbc:oracle:thin:@[ip]:[port]:[db]";

    // private String dbType;//数据库类型,mysql,oracle
    protected static DBType dbType = DBType.MYSQL;
    protected static String db = "test";//数据库中表的名称,如test数据库等
    protected static String userName = "root";//用户名
    protected static String passWord = "root";//密码
    protected static String ip = "127.0.0.1";
    protected static String port = "3306";
    protected static String driver = "com.mysql.jdbc.Driver";//驱动加载类 mysql :
    protected static String url = "jdbc:mysql://127.0.0.1:3306/driver?useUnicode=true&characterEncoding=UTF8";//连接mysql数据库的链接

    private DataBase DataBase;


    static {
        Map<String, String> dbConfigMap = PropertiesUtils.dbConfig;

        if (checkHaving("db", dbConfigMap)) {
            db = dbConfigMap.get("db");
        }
        if (checkHaving("username", dbConfigMap)) {
            userName = dbConfigMap.get("username");
        }
        if (checkHaving("password", dbConfigMap)) {
            passWord = dbConfigMap.get("password");
        }
        if (checkHaving("ip", dbConfigMap)) {
            ip = dbConfigMap.get("ip");
        }
        if (checkHaving("port", dbConfigMap)) {
            port = dbConfigMap.get("port");
        }
        if (checkHaving("dbType", dbConfigMap)) {
            String dbTypeStr = dbConfigMap.get("dbType");
            if (dbTypeStr.equals(DBType.MYSQL.getDescribe())) {
                dbType = DBType.MYSQL;
            } else if (dbTypeStr.equals(DBType.ORACLE.getDescribe())) {
                dbType = DBType.ORACLE;
            } else if (dbTypeStr.equals(DBType.PGSQL.getDescribe())) {
                dbType = DBType.PGSQL;
            } else if (dbTypeStr.equals(DBType.SQLSERVER.getDescribe())){
                dbType=DBType.SQLSERVER;
            }
        }
        //初始化
        initUrlAdapter();
    }

    //检查是dbconfig配置文件中否存在某一项的配置
    private static boolean checkHaving(String str, Map<String, String> map) {
        if (StringUtils.isNotBlank(map.get(str))) {
            return true;
        }
        return false;
    }

    public DataBaseFactory() {
    }

    /**
     * 得到数据库连接
     */
    public static void initUrlAdapter() {
        if (dbType.equals(DBType.MYSQL)) {
            driver = "com.mysql.jdbc.Driver";
            url = mysqlUrl.replace("[ip]", ip).replace("[port]", port).replace("[db]", db);
        } else if (dbType.equals(DBType.ORACLE)) {
            driver = "oracle.jdbc.driver.OracleDriver";
            url = oracleUrl.replace("[ip]", ip).replace("[port]", port).replace("[db]", db);
        }else if (dbType.equals(DBType.PGSQL)) {
            driver = "org.postgresql.Driver";
            url = pgsqlUrl.replace("[ip]", ip).replace("[port]", port).replace("[db]", db);
        }else if (dbType.equals(DBType.SQLSERVER)){
            driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
            url = sqlServerUrl.replace("[ip]", ip).replace("[port]", port).replace("[db]", db);
        }
        else {
            throw new RuntimeException("初始化链接数据库失败");
        }
    }


    /**
     * 创建基础数据对象
     *
     * @param userName
     * @param password
     * @param ip
     * @param port
     * @param db
     * @param dbType
     * @return
     */
    public static DataBase createDatabase(String userName, String password, String ip, String port, String db, DBType dbType) {
        return new DataBase(dbType, db, userName, passWord, driver, url);
    }

    /**
     * 用默认的配置 或者 配置文件中的配置
     *
     * @return
     */
    public static DataBase createDatabase() {
        return createDatabase(userName, passWord, ip, port, db, dbType);
    }

    public static String getMysqlUrl() {
        return mysqlUrl;
    }

    public static void setMysqlUrl(String mysqlUrl) {
        DataBaseFactory.mysqlUrl = mysqlUrl;
    }

    public static String getOracleUrl() {
        return oracleUrl;
    }

    public static void setOracleUrl(String oracleUrl) {
        DataBaseFactory.oracleUrl = oracleUrl;
    }

    public DBType getDbType() {
        return dbType;
    }

    public void setDbType(DBType dbType) {
        this.dbType = dbType;
    }

    public String getDb() {
        return db;
    }

    public void setDb(String db) {
        this.db = db;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public com.dxf.database.dbInfo.DataBase getDataBase() {
        return DataBase;
    }

    public void setDataBase(com.dxf.database.dbInfo.DataBase dataBase) {
        DataBase = dataBase;
    }

    /**
     * 查看所有表数据
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        // 1 创建工程
        DataBaseFactory dbFactory = new DataBaseFactory();
        //2 输入参数并创建DataBase
        DataBase database = dbFactory.createDatabase();
        //3 创建DB的工作对象
        DBMetaData dbMetaData = new DBMetaDataImpl();

        //4 获取数据库中的表信息
        List<Table> allTables = dbMetaData.getAllTables(database);
        //5 打印
        allTables.forEach(x -> System.out.println(x));
    }
}
